const question = require('./question.js')

Page({
  data: {
    showResult: false,
    form: {},
    radioItems: [
      {name: 'cell standard', value: 'A', checked: true},
      {name: 'cell standard', value: 'B'}
    ],
    // 最后被选中的值
    selectOneRadio: '',
    currentQuestionIndex: 0,
    questions: JSON.parse(JSON.stringify(question.discQuestion)),
    answers: [],
    checkboxItems: [],
    disc: {}
  },
  onLoad() {
    wx.setNavigationBarTitle({
      title: 'DISC测试',
    })
  },
  tapDialogButton({detail}) {
    console.log(detail.index === 1 ? '确定' : '取消')
    this.setData({
      'showDialog': false
    })
  },
  submit() {
    let answers = this.data.answers
    if (answers.length < this.data.questions.length) {
      wx.showToast({
        icon: 'error',
        title: '未完成所有题目',
      })
      return
    }
    let disc = {
      D: 0, I: 0, S: 0, C: 0
    }
    for (let i = 0; i < answers.length; i++) {
      if (answers[i] == null) {
        wx.showModal({
          title: '提交失败',
          content: '第' + (i + 1) + '题未答',
          showCancel: false,
          complete: (res) => {
          }
        })
        return
      }
      disc[answers[i]]++
    }
    this.data.showResult = true
    this.setData({
      showResult: this.data.showResult,
      disc: disc
    })
  },
  closeResult() {
    this.data.showResult = false
    this.setData({
      showResult: this.data.showResult,
    })
  },
  next() {
    this.data.currentQuestionIndex = (this.data.currentQuestionIndex + 1) % this.data.questions.length
    this.setData({
      currentQuestionIndex: this.data.currentQuestionIndex,
      questions: this.data.questions
    })
  },
  last() {
    this.data.currentQuestionIndex = (this.data.currentQuestionIndex - 1) % this.data.questions.length
    this.setData({
      currentQuestionIndex: this.data.currentQuestionIndex,
      questions: this.data.questions
    })
  },
  onShareAppMessage: function () {
    return {
      title: '多功能自测',
      path: '/pages/mainPage?id=123'
    }
  },
  checkboxChange: function (val) {
    this.data.answers[this.data.currentQuestionIndex] = val.detail
    this.setData({
      answers: this.data.answers
    })
  },
});